package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class PostEventPeer extends BasePeer{
	public static final String tableName = "PostEvent";
	public static final String EVENTID = "eventId";
	public static final String USERID = "userId";
	public static final String POSTID = "postId";
	public static final String REPLIEDID = "repliedId";
	public static final String ACTION = "action";
	public static final String EVENTTIME = "eventTime";
	public static final String[] columnNames=new String[]{"eventId","userId","postId","repliedId","action","eventTime"};
	public static PostEvent getPostEventByPKs(Object eventId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(PostEventPeer.tableName,PostEventPeer.columnNames," TRUE  AND `eventId`=? ",new Object[]{eventId,},0, 1,conn);
			PostEvent postEvent = new PostEvent();
		fill(rs,postEvent);
		return postEvent;
	}
	public static PostEvent getPostEventReferenceColumnsOnlyByPKs(Object eventId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(PostEventPeer.tableName,new String[]{"eventId",}," TRUE AND `eventId`=?",new Object[]{eventId,}, 0,  1,conn);
			PostEvent postEvent = new PostEvent();
		fill(rs,postEvent);
		return postEvent;
	}
	public static PostEvent getPostEventByEventId(long eventId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(PostEventPeer.tableName,PostEventPeer.columnNames,"`eventId`=?",new Object[]{eventId},0, 1,conn);
			PostEvent postEvent = new PostEvent();
		fill(rs,postEvent);
		return postEvent;
	}
	public static void fill(ResultSet rs, PostEvent postEvent) throws SQLException {
		if(rs.next()){
			try{postEvent.setEventId(rs.getLong("eventId"));}catch(Exception ex){}
			try{postEvent.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{postEvent.setPostId(rs.getLong("postId"));}catch(Exception ex){}
			try{postEvent.setRepliedId(rs.getLong("repliedId"));}catch(Exception ex){}
			try{postEvent.setAction(rs.getInt("action"));}catch(Exception ex){}
			try{postEvent.setEventTime(rs.getTimestamp("eventTime"));}catch(Exception ex){}
			postEvent.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<PostEvent> postEventCollection) throws SQLException {
		while(rs.next()){
			PostEvent postEvent=new PostEvent();
			try{postEvent.setEventId(rs.getLong("eventId"));}catch(Exception ex){}
			try{postEvent.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{postEvent.setPostId(rs.getLong("postId"));}catch(Exception ex){}
			try{postEvent.setRepliedId(rs.getLong("repliedId"));}catch(Exception ex){}
			try{postEvent.setAction(rs.getInt("action"));}catch(Exception ex){}
			try{postEvent.setEventTime(rs.getTimestamp("eventTime"));}catch(Exception ex){}
			postEvent.resetModified();
			postEventCollection.add(postEvent);
		}
	}
	public static boolean update(PostEvent postEvent, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < PostEventPeer.columnNames.length; i++) {
			if (((PostEventBase)postEvent).modified[i]) {
				columns.add(PostEventPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `PostEvent` SET ");
		sql.append("WHERE TRUE AND `eventId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,postEvent.get(column));
		}
		pstmt.setObject(columns.size()+1+0,postEvent.get("eventId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(PostEvent postEvent, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (postEvent.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `PostEvent` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (postEvent.get(column) != null) {
				pstmt.setObject(i + 1 + j, postEvent.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<PostEvent> getPostEventCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<PostEvent> collection=new Vector<PostEvent>();
		fill(rs,collection);
		return collection;
	}
}
